function distance(p1, p2)
{
    var xdiff = (p1.x-p2.x);
    var ydiff = (p1.y-p2.y)
    return Math.sqrt(xdiff*xdiff + ydiff*ydiff);
}

function Position(x, y)
{
    this.x = x;
    this.y = y;
}


var g_camera = 
{
    'w':480,
    'h':640,
    'pos' : new Position(0,0),
    'target' : new Position(0,0),
    'vel' : new Position(0,0),
    'speed' : 5,
    'panTo' : function(pos,speed)
    {
        //console.log("Camera: ", pos, this.vel);
        if(this.vel.x || this.vel.y)
            return;
        this.speed = speed;
        this.target = pos;  
        var d = distance(this.pos, this.target);
        this.vel.x = Math.round(speed*(pos.x-this.pos.x)/d);
        this.vel.y = Math.round(speed*(pos.y-this.pos.y)/d);
    },

    'update': function()
    {
        //console.log("Camera Update: ", this.pos, this.target, this.vel);
        if(this.pos.y <= this.target.y)
        {
            this.vel.x = 0;
            this.vel.y = 0;
            return false;
        }
        else
        {
            this.pos.x += this.vel.x;
            this.pos.y += this.vel.y;
            return true;
        }
    },

    'isInView':function(point)
    {
        //if(point.x > this.pos.x && point.x < (this.pos.x + this.w))
            if(point.y > this.pos.y && point.y < (this.pos.y+this.h))
                return true;
        return false;                
    },

    'getRelativePos': function(point)
    {
        var pos = [];
        pos.push(point.x-this.pos.x);
        pos.push(point.y-this.pos.y);
        return pos;
    },
    'getAbsolutePos': function(x,y)
    {
        var pos = [];
        pos.push(x + this.pos.x);
        pos.push(y + this.pos.y);
        return pos;
    }
};
